<template>
  <view class="auth">
    <button type="primary" plain open-type="getUserInfo"
      @click="handleGetUserInfo">
      获取授权
    </button>
  </view>
</template>

<script>
  import {
    login,
    requestPayment
  } from "../../api/index.js"
  import {
    setStorage,
    getStorage
  } from "../../api/Storage.js"
  export default {
    data() {
      return {
        provider: ''
      }
    },
    methods: {
      getToken(res) {
        const {
          encryptedData,
          rawData,
          iv,
          signature
        } = res
        if (!this.provider) {
          setStorage("token",
            "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo"
          );
          uni.navigateBack({
            delta: 1
          });
        } else {
          login().then(res => {
            const code = res.code
            const loginParams = {
              encryptedData,
              rawData,
              iv,
              signature,
              code
            }
            let token = ''
            this.$http({
              url: "/users/wxlogin",
              data: loginParams,
              method: "post"
            }).then(res => {
              console.log(res);
            })
            setStorage("token",
              "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo"
            );
            uni.navigateBack({
              delta: 1
            });
          })
        }

      },
      handleGetUserInfo(e) {
        uni.getProvider({
          service: 'oauth',
          success: (res) => {
            this.provider = res.provider[0]
            if (res.provider[0] === 'weixin') {
              uni.getUserProfile({
                desc: '用于完善会员资料',
                success: (res) => {
                  console.log(res);
                  setStorage('userinfo', res.userInfo)
                  this.getToken(res)
                  this.info = res
                }
              })
            } else if (res.provider.length === 0) {
              console.log('H5');
              setStorage('userinfo', {
                avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/xlJcwDV98mznLNsiaGtP9dDFLZOa6wxyUyHXJPZSgAIgiagGVWicbo4DU5AKkoCGLbUiaq4JqrU6icI43ewGHVNvcSg/132",
                city: "Wuhan",
                country: "China",
                gender: 1,
                language: "zh_CN",
                nickName: "丶",
                province: "Hubei",
              })
              this.getToken({})
            } else {
              uni.login({
                provider: res.provider[0],
                success: function(loginRes) {
                  console.log(loginRes.authResult);
                  // 获取用户信息
                  uni.getUserInfo({
                    provider: res.provider[0],
                    success: (infoRes) => {
                      setStorage('userinfo', infoRes.userInfo)
                      this.getToken(infoRes)
                    }
                  });
                }
              });
            }
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .auth {
    button {
      margin-top: 40upx;
      width: 80%;
    }
  }
</style>
